<template>
  <div class="app-container">
    <div v-permission="['team:create']" class="addbtn" @click="dialogTop('add')">添加用户</div>
    <div class="seatch">
      <el-input
        v-model="listQuery.keyword"
        placeholder="按操作人搜索"
        @keyup.enter.native="searchName($event)"
      >
        <i slot="prefix" class="el-input__icon el-icon-search" @click="getList()" />
      </el-input>
    </div>
    <div class="content">
      <el-table
        v-loading="listLoading"
        align="center"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="userName"
          align="center"
          label="用户名"
        />
        <el-table-column
          prop="name"
          align="center"
          label="姓名"
        />
        <el-table-column
          align="center"
          prop="teamName"
          label="所属组织"
        />
        <el-table-column
          align="center"
          prop="roleName"
          label="角色"
        />
        <el-table-column
          align="center"
          prop="telephone"
          label="手机号"
        />
        <el-table-column
          align="center"
          prop="createAt"
          label="操作"
          width="220"
        >
          <template slot-scope="{row}">
            <div class="operate">
              <!-- <div @click="dialogTop('add',row)">添加</div> -->
              <div v-permission="['team:edit']" @click="edgeDialog(row)">编辑</div>
              <!-- <div @click="detailDialog(row)">详情</div> -->
              <div v-permission="['team:create']" @click="deleteDialog(row)">删除</div>
              <div v-permission="['team:create']" @click="resetDialog(row)">重置密码</div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageination-div">
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
      </div>

    </div>
    <!-- 添加,编辑用户 -->
    <el-dialog :title="diaDelOrAdd?'添加用户':'编辑用户'" :visible.sync="dialogFormVisible" :close-on-click-modal="false" custom-class="customClass">
      <el-form ref="addForm" :model="addForm" :rules="rules" auto-complete="on" label-position="left" style="width: 97%;">
        <el-form-item label="用户名：" :label-width="formLabelWidth" prop="userName">
          <el-input
            v-model="addForm.userName"
            placeholder="请输入用户名"
            name="userName"
            maxlength="32"
            tabindex="1"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="姓名：" :label-width="formLabelWidth" prop="name">
          <el-input
            v-model="addForm.name"
            placeholder="请输入姓名"
            name="name"
            maxlength="32"
            tabindex="2"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="所属组织：" :label-width="formLabelWidth">
          <div class="textName">{{ teamName }}</div>
        </el-form-item>
        <el-form-item label="角色：" prop="roleId" :label-width="formLabelWidth">
          <el-select v-model="addForm.roleId" placeholder="请选择">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号：" :label-width="formLabelWidth" prop="telephone">
          <el-input
            v-model="addForm.telephone"
            placeholder="请输入手机号"
            name="telephone"
            maxlength="32"
            tabindex="3"
            autocomplete="off"
          />
        </el-form-item>
      </el-form>
      <div class="footer">
        <div class="cancel" @click="dialogTop('cancel')">
          <span>取消</span>
        </div>
        <div v-if="diaDelOrAdd" class="sure" @click="createData">
          <span>添加</span>
        </div>
        <div v-else class="sure" @click="updataData">
          <span>编辑</span>
        </div>
      </div>
    </el-dialog>

    <delete-dialog
      :dislog-name="deleteObiect.userName"
      :dialog-del-visible="dialogDelVisible"
      :title="'用户'"
      @funcancl="funcancl"
      @func="deleteData"
    />
    <reset-dialog
      :dialog-reset-visible="dialogResetVisible"
      @funsetcancl="funsetcancl"
      @funcreset="reseteData"
    />
  </div>

</template>

<script>
import Pagination from '@/components/pagination'
import DeleteDialog from '@/components/dialog/delete'
import ResetDialog from '@/components/dialog/reset'
import { getUserList, createUser, updateUser, delUser, resetPassword } from '@/api/user'
import { getRoleList } from '@/api/role'
export default {
  name: 'OrganizationUser',
  components: {
    Pagination,
    DeleteDialog,
    ResetDialog
  },
  props: {
    teamId: {
      type: Number,
      default: null
    },
    teamObject: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      tableData: [],
      total: 0,
      listLoading: true,
      listQuery: {
        teamId: this.teamId,
        keyword: null,
        pageNum: 1,
        pageSize: 10
      },
      dialogFormVisible: false,
      dialogDelVisible: false,
      dialogResetVisible: false,
      dialogDetailVisible: false,
      teamName: '',
      addForm: {
        userName: '',
        name: '',
        teamId: this.teamId,
        roleId: '',
        id: '',
        telephone: ''
      },
      deleteObiect: {},
      roleList: [],
      formLabelWidth: '120px',
      lablePosition: 'left',
      diaDelOrAdd: false,
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        userName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        roleId: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        telephone: [
          { required: true, message: '必填', trigger: 'change' },
          { pattern: /^1[34578]\d{9}$/, message: '手机号格式错误' }
        ]
      }
    }
  },
  watch: {
    teamId: function(value) {
      console.log()
      this.listQuery.teamId = value
      this.addForm.teamId = value
      this.getList()
    },
    teamObject: function(param) {
      this.teamName = param.name
    }
  },
  mounted() {
    this.getList()
    this.getRoleListData()
  },
  methods: {
    sizeChange(val) {
      this.pageNum = val
    },
    currentChange(val) {
      this.currentPage = val
    },
    searchName(e) {
      this.listQuery.keyword = e.target.value
      this.getList()
    },
    clean() {
      this.addForm = {
        userName: '',
        name: '',
        teamId: this.teamId,
        roleId: '',
        id: '',
        telephone: ''
      }
    },
    // 获取数据
    async getList() {
      const res = await getUserList(this.listQuery)
      if (res.code === 200) {
        this.tableData = res.data.list
        this.total = res.data.total || 0
        this.pages = res.data.pages || 0
        this.listLoading = false
      }
    },
    // 获取角色信息
    async getRoleListData() {
      const res = await getRoleList()
      if (res.code === 200) {
        this.roleList = res.data
      } else {
        console.log(res)
      }
    },
    dialogTop(val, row) {
      if (val === 'cancel') {
        this.diaDelOrAdd = false
        this.dialogFormVisible = false
      } else {
        // this.teamName = row.teamName
        this.diaDelOrAdd = true
        this.dialogFormVisible = true
      }
      this.clean()
      this.$nextTick(() => {
        this.$refs.addForm.clearValidate()
      })
    },
    edgeDialog(row) {
      this.diaDelOrAdd = false
      this.dialogFormVisible = true
      this.addForm = {
        userName: row.userName,
        name: row.name,
        teamId: row.teamId,
        roleId: row.roleId,
        id: row.id,
        telephone: row.telephone
      }
      this.$nextTick(() => {
        this.$refs.addForm.clearValidate()
      })
    },
    createData() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          const res = await createUser(this.addForm)
          if (res.code === 200) {
            this.dialogFormVisible = false
            this.addForm = {
              userName: '',
              name: '',
              teamId: this.teamId,
              roleId: '',
              id: '',
              telephone: ''
            }
            this.$message({
              type: 'success',
              message: '添加成功!',
              showClose: true,
              center: true,
              duration: 2 * 1000
            })
            this.getList()
          } else {
            console.log(res)
          }
        }
      })
    },
    updataData() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          const res = await updateUser(this.addForm)
          if (res.code === 200) {
            this.dialogFormVisible = false
            this.addForm = {
              userName: '',
              name: '',
              teamId: this.teamId,
              roleId: '',
              id: '',
              telephone: ''
            }
            this.$message({
              type: 'success',
              message: '编辑成功!',
              showClose: true,
              center: true,
              duration: 2 * 1000
            })
            this.getList()
          } else {
            this.dialogFormVisible = false
          }
        }
      })
    },
    // 删除
    deleteDialog(row) {
      this.dialogDelVisible = true
      this.deleteObiect = row
    },
    resetDialog(row) {
      this.dialogResetVisible = true
      this.deleteObiect = row
    },
    async deleteData() {
      const id = this.deleteObiect.id
      const res = await delUser(id)
      if (res.code === 200) {
        this.getList()
        this.dialogDelVisible = false
        this.$message({
          type: 'success',
          message: '删除成功!',
          showClose: true,
          center: true,
          duration: 2 * 1000
        })
      } else {
        this.dialogDelVisible = false
      }
    },
    async reseteData() {
      const id = this.deleteObiect.id
      const res = await resetPassword(id)
      if (res.code === 200) {
        this.getList()
        this.dialogResetVisible = false
        this.$message({
          type: 'success',
          message: '密码重置成功!',
          showClose: true,
          center: true,
          duration: 2 * 1000
        })
      } else {
        console.log(res)
      }
    },
    funcancl(val) {
      this.dialogDelVisible = false
    },
    funsetcancl(val) {
      this.dialogResetVisible = false
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container{
  margin: 0;
  min-height: 700px;
  .seatch{
    right: 20px;
  }
  .textName{
    font-size:14px;
    font-weight:400;
    color:rgba(247,181,0,0.8);
  }
}
</style>
